#include <unittest++/UnitTest++.h>
#include <string>
#include <vector>

#include "stringoperations.h"

using std::string;
using std::vector;

SUITE(StringOperationsTest)
{
    TEST(TestLowerCase)
    {
    	string testString = "TESTSTRING";
		StringOperations::lowercase(testString);
		CHECK_EQUAL("teststring", testString);
		
		testString = "teststring";
		StringOperations::lowercase(testString);
		CHECK_EQUAL("teststring", testString);

		testString = "~!@#$%^&*()_1234567890-";
		StringOperations::lowercase(testString);
		CHECK_EQUAL("~!@#$%^&*()_1234567890-", testString);
		
		testString = "H_ell_O";
		StringOperations::lowercase(testString);
		CHECK_EQUAL("h_ell_o", testString);
    }
	
	TEST(TestDos2Unix)
    {
		string testString = "abcde\r\nfgs\r\r\n";
		StringOperations::dos2unix(testString);
		CHECK_EQUAL("abcde\nfgs\r\n", testString);
		
		testString = "\r\n";
		StringOperations::dos2unix(testString);
		CHECK_EQUAL("\n", testString);
		
		testString = "teststring";
		StringOperations::dos2unix(testString);
		CHECK_EQUAL("teststring", testString);
	}
	
	TEST(TestReplace)
    {
		string testString = "abcaabbabbab";
		StringOperations::replace(testString, "ab", "a");
		CHECK_EQUAL("acaababa", testString);
		
		testString = "stringstringstring";
		StringOperations::replace(testString, "stringstring", "string");
		CHECK_EQUAL("stringstring", testString);
	}
	
	TEST(TestTokenize)
    {
		string testString = "A-B-C";
		vector<string> tokenized;
        StringOperations::tokenize(testString, "-", tokenized);
		CHECK_EQUAL(3, tokenized.size());
		CHECK_EQUAL("A", tokenized[0]);
		CHECK_EQUAL("B", tokenized[1]);
		CHECK_EQUAL("C", tokenized[2]);
				
		testString = "A_*_B_*_C";
		StringOperations::tokenize(testString, "_*_", tokenized);
		CHECK_EQUAL(3, tokenized.size());
		CHECK_EQUAL("A", tokenized[0]);
		CHECK_EQUAL("B", tokenized[1]);
		CHECK_EQUAL("C", tokenized[2]);
		
		testString = "A_*_B_*_C";
		StringOperations::tokenize(testString, "_**_", tokenized);
        CHECK_EQUAL(1, tokenized.size());
		CHECK_EQUAL("A_*_B_*_C", tokenized[0]);
	}
    
    TEST(TestToNumeric)
    {
        int integer;
		string numericString = "42";
        StringOperations::toNumeric(numericString, integer);
		CHECK_EQUAL(42, integer);
        
        numericString = "-42";
        StringOperations::toNumeric(numericString, integer);
		CHECK_EQUAL(-42, integer);
        
        float floatingPoint;
        numericString = "42.0001";
        StringOperations::toNumeric(numericString, floatingPoint);
		CHECK_CLOSE(42.0001f, floatingPoint, 0.00001);
        
        numericString = "-42.0001";
        StringOperations::toNumeric(numericString, floatingPoint);
		CHECK_CLOSE(-42.0001f, floatingPoint, 0.00001);
	}
}
